﻿/*
BF3 RCON Simulator simulates the RCON interface of a BF3 server.
Copyright (C) 2011 Timi

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

You can contact us at http://bf3sim.codeplex.com/.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Battlefield3;
using System.Net.Battlefield3.Server;

namespace BF3RconSimulator
{
    //will be the main class that handles everything
    //the mainform will be supplied to this, mainly for logging simplicity
    class RconSimulator
    {
        internal MainForm MainForm;
        internal RconServer Server;

        internal SimulateHelpers Simulate { get; private set; }

        internal VarsData Vars { get; private set; }

        internal RconSimulator(MainForm mainForm, IPAddress address, int port)
        {
            MainForm = mainForm;

            Server = new RconServer(address, port);
            Server.Request += new EventHandler<RequestEventArgs>(Server_Request);

            Simulate = new SimulateHelpers(this);
            Vars = new VarsData(this);
        }

        void Server_Request(object sender, RequestEventArgs e)
        {
            string command = e.RequestWords[0].ToLowerInvariant();
            var words = e.RequestWords;

            if (command.Substring(0, 5) == "vars.")
            {
                e.ResponseWords = words.Count == 1 ?
                    Vars.GetVar(command).Words : //get
                    Vars.SetVar(command, words[1]).Words; //set
            }
        }

        internal void Start() { Server.Start(); }

        internal void Stop() { Server.Stop(); }
    }
}
